package edu.hrbu.spring.exception.resolver.controller;

import edu.hrbu.spring.exception.BusinessException;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
 * @author 徐登宇
 */
@ControllerAdvice
public class GlobalHandlerExceptionResolverController {

    // 处理算术异常
    @ExceptionHandler(ArithmeticException.class)
    public String processArithmeticException(ArithmeticException ex, Model model) {
        model.addAttribute("ex", ex);
        return "error";
    }

    // 处理业务异常
    @ExceptionHandler(BusinessException.class)
    public String processBusinessException(BusinessException ex, Model model) {
        model.addAttribute("ex", ex);
        return "error";
    }

    // 处理其他的异常（优先匹配具体的异常，如果出现的异常其他方法都没法匹配上，那么会执行这个异常）
    @ExceptionHandler(Exception.class)
    public String processElseException(Exception ex, Model model) {
        model.addAttribute("ex", ex);
        return "error";
    }

}
